
if ($.fn.accordion) {
    if ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) {
        $.fn.accordion.defaults.animate = false;
    }
}
if ($.fn.tree) {
    if ($.browser.msie && ($.browser.version == "6.0" || $.browser.version == "7.0")) {
        $.fn.tree.defaults.animate = false;
    }
}
if ($.fn.pagination) {
    $.fn.pagination.defaults.beforePageText = '第';
    $.fn.pagination.defaults.afterPageText = '共{pages}页';
    $.fn.pagination.defaults.displayMsg = '显示{from}到{to},共{total}记录';
}
if ($.fn.datagrid) {
    $.fn.datagrid.defaults.loadMsg = '正在处理，请稍待。。。';
}
if ($.fn.treegrid && $.fn.datagrid) {
    $.fn.treegrid.defaults.loadMsg = $.fn.datagrid.defaults.loadMsg;
}
if ($.messager) {
    $.messager.defaults.ok = '确定';
    $.messager.defaults.cancel = '取消';
}
if ($.fn.validatebox) {
    $.fn.validatebox.defaults.missingMessage = '该输入项为必输项';
    $.fn.validatebox.defaults.rules.email.message = '请输入有效的电子邮件地址';
    $.fn.validatebox.defaults.rules.url.message = '请输入有效的URL地址';
    $.fn.validatebox.defaults.rules.length.message = '输入内容长度必须介于{0}和{1}之间';
    $.fn.validatebox.defaults.rules.remote.message = '请修正该字段';
    $.extend($.fn.validatebox.defaults.rules, {
        notEmpty: {
            validator: function (value, param) {
                return (value != "");
            },
            message: '必填项，不能为空。'
        },
        number: {
            validator: function (value, param) {
                var reg = /^[0-9]*$/;
                return reg.test(value);
            },
            message: '只允许输入数字。'
        },
        even: {
            validator: function (value, param) {
                var reg = /^[0-9]*$/;
                if (reg.test(value)) {
                    var num = parseInt(value);
                    if (num > 0 && num % 2 == 0) {
                        return true;
                    }
                    return false;
                }
                return false;
            },
            message: '只允许输入大于0的偶数。'
        },
		loginName: {
            validator: function (value, param) {
                var reg = /^[a-zA-Z0-9_]{3,16}$/;
                if (reg.test(value)) {
					return true;
                }
                return false;
            },
            message: '用户名由字母数字下划线组成，长度为3到16位'
        },        
        day: {
            validator: function (value, param) {
                var t = parseInt(value, 10);
                if (isNaN(t)) {
                    return false;
                }
                if (t >= 1 && t <= 31) {
                    return true;
                }
                return false;
            },
            message: '只允许输入日期数字。'
        },
        decimal: {
            validator: function (value, param) {
                var p = param ? param[0] : "";
                var reg = null;
                if (p) {
                    reg = new RegExp("^\\d+(\\.\\d{1," + p + "})?$");
                }
                else {
                    reg = new RegExp("^\\d+(\\.\\d+)?$");
                }
                return reg.test(value);
            },
            message: '只允许输入数字。'
        },
        money: {
            validator: function (value, param) {
                //var reg = /^\d+(\.\d{0,2})?$/;
                var reg = /^(-)?(([1-9]{1}\d*)|([0]{1}))(\.(\d){1,2})?$/;
                return reg.test(value);
            },
            message: '只允许输入金额。'
        },
        html: {
            validator: function (value, param) {
                var reg = /^.+\.html|\.htm$/;
                return reg.test(value.toLowerCase());
            },
            message: '只允许导入网页文件（htm、html 格式）。'
        },
        minLength: {
            validator: function (value, param) {
                return value.length >= param[0];
            },
            message: '最小长度不允许小于{0}位'
        }
    });
}
if ($.fn.numberbox) {
    $.fn.numberbox.defaults.missingMessage = '该输入项为必输项';
}
if ($.fn.combobox) {
    $.fn.combobox.defaults.missingMessage = '该输入项为必输项';
}
if ($.fn.combotree) {
    $.fn.combotree.defaults.missingMessage = '该输入项为必输项';
}
if ($.fn.combogrid) {
    $.fn.combogrid.defaults.missingMessage = '该输入项为必输项';
}
if ($.fn.calendar) {
    $.fn.calendar.defaults.weeks = ['日', '一', '二', '三', '四', '五', '六'];
    $.fn.calendar.defaults.months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
}
if ($.fn.datebox) {
    $.fn.datebox.defaults.currentText = '今天';
    $.fn.datebox.defaults.closeText = '关闭';
    $.fn.datebox.defaults.okText = '确定';
    $.fn.datebox.defaults.missingMessage = '该输入项为必输项';
    $.fn.datebox.defaults.formatter = function (date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();
        return y + '-' + (m < 10 ? ('0' + m) : m) + '-' + (d < 10 ? ('0' + d) : d);
    };
    $.fn.datebox.defaults.parser = function (s) {
        if (!s) return new Date();
        var ss = s.split('-');
        var y = parseInt(ss[0], 10);
        var m = parseInt(ss[1], 10);
        var d = parseInt(ss[2], 10);
        if (!isNaN(y) && !isNaN(m) && !isNaN(d)) {
            return new Date(y, m - 1, d);
        } else {
            return new Date();
        }
    };
}
if ($.fn.datetimebox && $.fn.datebox) {
    $.extend($.fn.datetimebox.defaults, {
        currentText: $.fn.datebox.defaults.currentText,
        closeText: $.fn.datebox.defaults.closeText,
        okText: $.fn.datebox.defaults.okText,
        missingMessage: $.fn.datebox.defaults.missingMessage
    });
}


if ($.fn.datagrid && $.fn.datagrid.defaults && $.fn.datagrid.defaults.editors) {
    //为EASYUI的datagrid附加上更多编辑功能
    $.extend($.fn.datagrid.defaults.editors, {
        url: {
            init: function (container, options) {
                var rowIndex = -1;
                var grid = container;
                while (!grid.hasClass("datagrid-view")) {
                    grid = grid.parent();
                }
                grid = grid.children("table:hidden");
                var row = grid.propertygrid("getSelected");
                //alert(row.value);
                var dialog = null;
                //alert(options)
                var html = $("<table class='more'><tr><td width='99%'><label class='ellipsis moreContext'></label></td><td nowrap='nowrap'><a style='color:blue' href='javascript:void(0)' >" + (options.readonly ? "查看" : "编辑") + "..</a></td></tr></table>").appendTo(container);
                html.bind("click", function (evt) {
                    var w = options.width || 400;
                    var h = options.height || 400;
                    var minimizable = options.minimizable || false;
                    var maximized = options.maximized || false;
                    var resizable = options.resizable == false ? false : true;
                    var draggable = options.draggable == false ? false : true;
                    var title = (options.title ? options.title : "属性") + (options.readonly ? "查看" : "编辑");
                    var cancelButton = options.cancelButton;
                    var url = options.url;
                    if (options.readonly) {
                        url += (url.indexOf("?") > 0 ? "&" : "?") + "readonly=1";
                    }
                    var win = window.self;
                    if (options.windowLevel) {
                        win = window[options.windowLevel];
                    }
                    var data = grid.propertygrid("getData") || {};
                    var owner = data.owner;
                    if ($.isFunction(url)) {
                        var rows = grid.datagrid("getData").rows;
                        url = url(rows);
                    }
                    dialog = win.OpenDialog({
                        "width": w,
                        "height": h,
                        "opener": window.self,
                        "minimizable": minimizable,
                        "maximized": maximized,
                        "draggable": draggable,
                        "title": title,
                        "cancelButton": options.cancelButton || true,
                        "close": options.close,
                        "url": url,
                        "dialogArgs": row.value,
                        "dialogArgs2": owner,
                        "isClose": true,
                        "isFrame": true,
                        "resizable": resizable,
                        "windowLevel": options.windowLevel,
                        "buttons": [{ "text": "确定", "iconCls": "icon-ok", "handler": function () { OkButtonClick(grid, dialog, options); dialog.dialog("close"); return false; } }]
                    });
                    return false;
                });
                return html;
            },
            getValue: function (target) {
                var label = $(target).find("label");
                return label.data("val");
            },
            setValue: function (target, value) {
                var label = $(target).find("label");
                label.data("val", value);
            },
            resize: function (target, width) {
                var target = $(target);
                var label = target.find("label");
                var link = target.find("a");
                if ($.boxModel == true) {
                    target.width(width - (target.outerWidth() - target.width()));
                    label.width(target.width() - link.outerWidth() - 10);
                } else {
                    target.width(width);
                    label.width(width - link.outerWidth() - 10);
                }
            }
        }
    });
    //不可修改只读编辑器
    $.extend($.fn.datagrid.defaults.editors, {
        readonly: {
            init: function (container, options) {
                var html = $("<table><tr><td><label class='ellipsis' style='color:gray'></label></td></tr></table>").appendTo(container);
                return html;
            },
            getValue: function (target) {
                var label = $(target).find("label");
                return label.data("val");
                //return label.html();
            },
            setValue: function (target, value) {
                var label = $(target).find("label");
                label.data("val", value);
                label.html(value.toString());
            },
            resize: function (target, width) {
                var target = $(target);
                var label = target.find("label");
                if ($.boxModel == true) {
                    target.width(width - (target.outerWidth() - target.width()));
                    label.width(target.width());
                } else {
                    target.width(width);
                    label.width(width);
                }
            }
        }
    });

    //不可修改只读编辑器
    $.extend($.fn.datagrid.defaults.editors, {
        htmlCodeText: {
            init: function (container, options) {
                var text = $("<input type=\"text\" class=\"datagrid-editable-input\">").appendTo(container);
                return text;
            },
            getValue: function (target) {
                var str = $(target).val();
                str = str.replace(/&/g, "&amp;");
                str = str.replace(/</g, "&lt;");
                str = str.replace(/>/g, "&gt;");
                return str;
            },
            setValue: function (target, value) {
                var str = value;
                str = str.replace(/&lt;/g, "<");
                str = str.replace(/&gt;/g, ">");
                str = str.replace(/&amp;/g, "&");
                $(target).val(str);
            },
            resize: function (target, width) {
                var jq = $(target);
                if ($.boxModel == true) {
                    jq.width(width - (jq.outerWidth() - jq.width()));
                } else {
                    jq.width(width);
                }
            }
        }
    });

    //偶数编辑器
    $.extend($.fn.datagrid.defaults.editors, {
        even: {
            init: function (container, options) {
                var input = $('<input type="text" class="datagrid-editable-input">').appendTo(container);
                input.numberbox(options);
                input.validatebox({
                    validType: "even"
                });
                return input;
            },
            destroy: function (target) {
                $(target).numberbox('destroy');
            },
            getValue: function (target) {
                $(target).blur();
                return $(target).numberbox('getValue');
            },
            setValue: function (target, value) {
                $(target).numberbox('setValue', value);
            },
            resize: function (target, width) {
                $(target)._outerWidth(width);
            }
        }
    });

    function OkButtonClick(grid, dialog, options) {
        var view = grid.parent();
        var row = grid.propertygrid("getSelected");
        var index = grid.propertygrid("getRowIndex", row);
        grid.propertygrid("beginEdit", index);
        var iframe = dialog.find("iframe");
        if (iframe.length > 0) {
            var win = iframe.get(0).contentWindow;
            if ($.isFunction(win.getReturnValue)) {
                var returnVal = win.getReturnValue();
                var tr = view.find("tr[datagrid-row-index=" + index + "]");
                var table = tr.find("table.more");
                $.fn.datagrid.defaults.editors.url.setValue(table, returnVal);
            }
        }
        grid.propertygrid("endEdit", index);
    }
}

var c = 0;
//扩展验证功能

if ($.fn.validatebox && $.fn.validatebox.defaults) {
    $.extend($.fn.validatebox.methods, {
        remove: function (jq, newposition) {
            return jq.each(function () {
                $(this).removeClass("validatebox-text validatebox-invalid").unbind('focus').unbind('blur');
            });
        },
        reduce: function (jq, newposition) {
            return jq.each(function () {
                var opt = newposition || $(this).data().validatebox.options;
                $(this).addClass("validatebox-text").validatebox(opt);
            });
        }
    });

    $.extend($.fn.validatebox.defaults.rules, {
        regExp: {
            validator: function (value, param) {
                var reg = param[0];
                if (value.match(reg)) {
                    return true;
                }
                else {
                    return false;
                }
            },
            message: '输入值与正则表达式{0}不匹配'
        }
    });
    $.extend($.fn.validatebox.defaults.rules, {
        //正则
        regExp: {
            validator: function (value, param) {
                var reg = param[0];
                if (value.match(reg)) {
                    return true;
                }
                else {
                    return false;
                }
            },
            message: '输入正则表达式{0}不符合要求'
        },
        customer: {
            validator: function (value, param) {
                var p = null;
                if (param[0]) {
                    p = eval(param[0]);
                }
                if ($.isFunction(p)) {
                    var result = p(value, param[1]);
                    if (result.success) {
                        return true;
                    }
                    else {
                        $.fn.validatebox.defaults.rules.customer.message = result.message || $.fn.validatebox.defaults.rules.customer.message;
                        return false;
                    }
                }
                return true;
            },
            message: '输入错误'
        },
        //唯一
        unique: {
            validator: function (value, param) {
                var p = param[0];
                var domain = p.domain;
                var uqkey = p.key;
                if (domain && uqkey) {
                    if (!$(window).data(domain)) {
                        $(window).data(domain, []);
                    }
                    var array = $(window).data(domain);
                    for (var i = 0; i < array.length; i++) {
                        var v = array[i];
                        if (v.uqkey != uqkey) {
                            if (v.value == value) {
                                return false;
                            }
                        }
                    }
                    return true;
                }
            },
            message: '输入值存在重复'
        },
        //多验证器
        many: {
            validator: function (value, param) {
                var rules = param;
                for (var i = 0; i < rules.length; i++) {
                    var rule = rules[i];
                    var ruleGroup = /([a-zA-Z_]+)(.*)/.exec(rule);
                    var ruleName = ruleGroup[1];
                    var ruleParam = eval(ruleGroup[2]);
                    if (!$.fn.validatebox.defaults.rules[ruleName]["validator"](value, ruleParam)) {
                        var message = $.fn.validatebox.defaults.rules[ruleName].message;
                        if (ruleParam && $.isArray(ruleParam)) {
                            for (var i = 0; i < ruleParam.length; i++) {
                                message = message.replace(new RegExp("\\{" + i + "\\}", "g"), ruleParam[i]);
                            }
                        }
                        $.fn.validatebox.defaults.rules.many.message = message;
                        return false;
                    }
                }
                return true;
            },
            message: ''
        }
    });
}

//扩展datagrid动态编辑器功能
if ($.fn.datagrid.methods) {
    $.extend($.fn.datagrid.methods, {
        "disableEditor": function (jq, param) {
            var field = param;
            if (field) {
                var d = $(jq);
                var e = d.datagrid('getColumnOption', field);
                if (!d.data("editors")) {
                    d.data("editors", {});
                }
                if (!d.data("editors")["@" + field]) {
                    d.data("editors")["@" + field] = e.editor;
                }
                e.editor = {};
            }
        },
        "enableEditor": function (jq, param) {
            var field = param;
            if (field) {
                var d = $(jq);
                var e = d.datagrid('getColumnOption', field);
                if (!d.data("editors")) {
                    d.data("editors", {});
                }
                if (d.data("editors")["@" + field]) {
                    e.editor = d.data("editors")["@" + field];
                }

            }
        }
    });
}